package com.example.imtoken.modules.main.wallet.details.trade.transfer.set_price

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.utils.Utils
import com.example.imtoken.entitys.http.GasEntity
import com.example.imtoken.entitys.http.Step
import java.util.*

class SettingMinerFeeViewModel : BaseViewModel() {

    private val gasEntity = MutableLiveData<GasEntity>()

    private val stepEntity = MutableLiveData<MutableList<Step>>()

    private val minerFee = MutableLiveData<String>()

    var oldP = 0

    fun getMinerFee(): MutableLiveData<String> {
        return minerFee
    }

    fun getStepEntity(): MutableLiveData<MutableList<Step>> {
        return stepEntity
    }

    fun setGasEntityValue(gasEntity: GasEntity) {
        oldP = gasEntity.defPosition
        this.gasEntity.value = gasEntity
        minerFee.value = Utils.calculate(gasEntity)
        stepEntity.value = gasEntity.steps
    }

    fun getGasEntity(): MutableLiveData<GasEntity> {
        return gasEntity
    }

    fun updateSelected(position: Int) {
        if (oldP == position) return
        val data = ArrayList<Step>()
        stepEntity.value?.forEach {
            val item = it.copy()
            item.isSelected = it.isSelected
            data.add(item)
        }
        data[oldP].isSelected = false
        data[position].isSelected = true
        stepEntity.value = data
        oldP = position

        val gasEntity = this.gasEntity.value
        gasEntity?.defPosition = position
        this.gasEntity.value = gasEntity

        minerFee.value = Utils.calculate(gasEntity)
    }

}